package com.example.redstarcommerce.cn.dh.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.shopcart.mapper.ShopCartMapper;
import com.example.redstarcommerce.cn.dh.shopcart.pojo.ShopCart;
import com.example.redstarcommerce.cn.dh.shopcart.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName ShopServiceImpl
 * @Author AndyWu
 * @Date 2024-11-01 22:40
 * @Motto 学不死就往死里学
 * @Version 1.0
 *
 * 在此多说一下此处功能我们需要的操作
 * 1. 添加商品到购物车
 *2. 更新购物车中的商品数量
 *3. 删除购物车中的商品
 * 4. 查看用户的购物车
 * 5. 清空购物车
 * 6. 计算购物车总价
 *7. 应用折扣码，使用优惠卷汇总
 *8. 创建订单
 * 9. 支付订单
 */

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;


    @Override
    public Result<List<ShopCart>> getAll() {
        List<ShopCart> shopCarts = shopCartMapper.selectList(null);
        if (shopCarts.isEmpty()){
            return Result.fail("购物车为空");
        }
        Result<List<ShopCart>> success = Result.success(shopCarts);
        return success;


    }

    @Override
    public Result<Boolean> deleteShopCart(String uid) {
       QueryWrapper  queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("uid",uid);
       int delete = shopCartMapper.delete(queryWrapper);
       if (delete <= 0){
           return Result.fail("删除失败");
       }
       return Result.success(true);

    }

    @Override
    public Result<Boolean> insertShopCart(String name, String type, String introduce, Float price, String map) {
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();

        Timestamp timeend = new Timestamp(System.currentTimeMillis());

        ShopCart shopCart = new ShopCart(uid,name, type, introduce, price,timeend, map);
        int insert = shopCartMapper.insert(shopCart);
        if (insert <= 0){
            return Result.fail(false);
        }
        return Result.success(true);

    }

    @Override
    public Result<Boolean> updateShopCart(String uid, String name, String type, String introduce, Float price, String map) {
        Timestamp timeend = new Timestamp(System.currentTimeMillis());
        ShopCart shopCart = new ShopCart(uid,name, type, introduce, price,timeend, map);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        int update = shopCartMapper.update(shopCart, queryWrapper);
        if (update <= 0){
            return Result.fail("更新失败");
        }
        return Result.success(true);




    }


}
